package logic

import (
	"context"
	"encoding/json"
	"jd-mall/jd-srv/cache"
	"jd-mall/jd-srv/model"
	"time"

	"jd-mall/jd-srv/internal/svc"
	"jd-mall/jd-srv/jd"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopShowLogic {
	return &ShopShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品详情
func (l *ShopShowLogic) ShopShow(in *jd.ShopShowRequest) (*jd.ShopShowResponse, error) {
	// todo: add your logic here and delete this line
	get := cache.Rdb.Get(l.ctx, "shop_Show")
	var shop model.Shop
	if get.Val() == "" {
		model.DB.Where("id=?", in.Id).Find(&shop)
		marshal, _ := json.Marshal(shop)
		cache.Rdb.Set(l.ctx, "shop_Show", marshal, time.Minute*10)
	} else {
		json.Unmarshal([]byte(get.Val()), &shop)
	}

	return &jd.ShopShowResponse{
		Name:  shop.Name,
		Price: int64(shop.Price),
		Num:   int64(shop.Num),
		Stock: int64(shop.Stock),
		Img:   shop.Img,
		State: int64(shop.State),
	}, nil
}
